package com.platform.modules.wallet.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.platform.common.shiro.ShiroUtils;
import com.platform.common.shiro.utils.Md5Utils;
import com.platform.common.version.ApiVersion;
import com.platform.common.version.VersionEnum;
import com.platform.common.web.controller.BaseController;
import com.platform.common.web.domain.AjaxResult;
import com.platform.modules.chat.domain.ChatUser;
import com.platform.modules.chat.service.ChatUserService;
import com.platform.modules.wallet.domain.Wallet;
import com.platform.modules.wallet.service.WalletAccountService;
import com.platform.modules.wallet.service.WalletCashService;
import com.platform.modules.wallet.service.WalletService;
import com.platform.modules.wallet.vo.CashVo01;
import com.platform.modules.wallet.vo.WalletVo01;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 钱包
 */
@RestController
@Slf4j
@RequestMapping("/wallet")
public class WalletController extends BaseController {


    @Resource
    private WalletService walletService;
    @Resource
    private WalletCashService walletCashService;
    @Resource
    private ChatUserService chatUserService;
    @Resource
    private WalletAccountService walletAccountService;

    /**
     * 获取钱包信息
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getWallet")
    public AjaxResult getWallet() {
        Long userId = ShiroUtils.getUserId();
        Wallet wallet = walletService.getWallet(userId);

        return AjaxResult.success(BeanUtil.toBean(wallet, WalletVo01.class));
    }

    /**
     * 我的账本记录
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getMyAccount")
    public AjaxResult getMyAccount() {
        Long userId = ShiroUtils.getUserId();
        return AjaxResult.success(walletAccountService.queryByUserId(userId));
    }

    /**
     * 提现
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/cash")
    public AjaxResult cash(@Validated @RequestBody CashVo01 cashVo) {

        Long userId = ShiroUtils.getUserId();
        // 当前用户
        ChatUser cu = chatUserService.getById(userId);
        Wallet wallet = walletService.getWallet(userId);
        if (StrUtil.isBlank(wallet.getPayPassword())) {
            return AjaxResult.fail("提现失败：支付密码未设置");
        }
        String payPassword =Md5Utils.credentials(cashVo.getPayPassword(), cu.getSalt());
        if (!payPassword.equalsIgnoreCase(wallet.getPayPassword())) {
            return AjaxResult.fail("提现失败：密码验证有误");
        }

        //元转换为分
        int amount = cashVo.getMoney().multiply(BigDecimal.valueOf(100)).intValue();
        cashVo.setAmount(Integer.valueOf(amount));
        cashVo.setUserId(userId);

        if (wallet.getAmount().intValue() < cashVo.getAmount().intValue()) {
            return AjaxResult.fail("失败：提现金额超出余额");
        }

        int maxCashAmount = 2000;
        if (cashVo.getAmount().intValue() > maxCashAmount) {
            return AjaxResult.fail("失败：提现金额超出" + maxCashAmount);
        }

        walletCashService.createCash(cashVo);

        return AjaxResult.success("提现订单创建成功");
    }

}
